<?php


/**
 * PDF Invoicing Solution for VirtueMart & Joomla!
 * 
 * @package   VM Invoice
 * @version   2.0.24
 * @author    ARTIO http://www.artio.net
 * @copyright Copyright (C) 2011 ARTIO s.r.o. 
 * @license   GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
 */

defined('_JEXEC') or die('Restrict Access');

jimport('joomla.application.component.controller');

class VMInvoiceControllerOrder extends JController
{

    function __construct($config = array())
    {
        parent::__construct($config);
        $this->registerTask('userajax', 'display');
        $this->registerTask('orderajax', 'display');
    }

    function couponajax()
    {
    	header('Content-Type: text/xml; charset=UTF-8');
    	$model = $this->getModel('order');
    	echo $model->getAjaxCoupon(JRequest::getVar('coupon'),JRequest::getVar('currency'));
    	exit;
    }
    
    function statesajax()
    {
    	header('Content-Type: text/xml; charset=UTF-8');
    	
    	$states = InvoiceGetter::getStates(JRequest::getVar('country_id'));
    	foreach ($states as $state)
    	{
    		echo '<option value="'.$state->id.'">'.$state->name.'</option>'."\n";
    	}
    	
    	exit;
    }
     
    function whisper()
    {
        $type = JRequest::getString('type');
        $model = $this->getModel('order');
        /* @var $model VMInvoiceModelOrder */
        die(JHTML::_('select.genericlist', $model->getAjaxList(JRequest::getString('str'), $type), 'naseptavac', 'multiple="multiple" onclick="getClickHandler($(\'' . $type . '\'));" onchange="getChangeHandler($(\'' . $type . '\'));"', 'id', 'name'));
    }

    function display($cachable = false, $urlparams = false)
    {
        JRequest::setVar('view', 'order');
        parent::display($cachable, $urlparams);
    }

    function apply()
    {
        $this->save(true);
    }

    function save($apply = false)
    {
        $model = $this->getModel('order');
        /* @var $model VMInvoiceModelOrder */
        $id = $model->save(JRequest::get('post',4)); //4 = allow HTML
        if ($apply)
            $this->setRedirect('index.php?option=com_vminvoice&controller=invoices&task=editOrder&cid=' . $id, JText::_('COM_VMINVOICE_ORDER_SAVED'));
        else
            $this->cancel('Order saved');
    }

    function cancel($msg = 'Order edit canceled')
    {
        $this->setRedirect('index.php?option=com_vminvoice&controller=invoices', JText::_($msg));
    }
}

?>